package com.freight.base.serviceimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.freight.base.dao.CityDao;
import com.freight.base.entity.City;
import com.freight.base.mapper.AreaCityMapper;
import com.freight.base.service.CityService;
import com.freight.base.vo.AreaCityVo;
import com.freight.base.vo.CityListVo;
import com.freight.base.vo.CityVo;
import com.freight.core.common.vo.SearchVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.*;

/**
 * 城市维表接口实现
 *
 * @author huyingtai
 */
@Slf4j
@Service
@Transactional
public class CityServiceImpl implements CityService {

    @Autowired
    private CityDao cityDao;

    @Autowired
    private AreaCityMapper areaCityMapper;

    @Override
    public CityDao getRepository() {
        return cityDao;
    }

    @Override
    public List<AreaCityVo> getAreaCityList(String code, String type) {
        if(type.equals("1")){
            return areaCityMapper.getCityPage(code);
        }else {
            return areaCityMapper.getAreaPage(code);
        }
    }

    @Override
    public List<CityListVo> getCityList(String code) {
        return cityDao.getCityList(code);
    }

    @Override
    public Page<City> findByCondition(City city, SearchVo searchVo, Pageable pageable) {

        return cityDao.findAll(new Specification<City>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<City> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");
                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<String> cityCode = root.get("cityCode");
                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<String> cityName = root.get("cityName");
                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<String> provinceCode = root.get("provinceCode");
                List<Predicate> list = new ArrayList<>();

                // 创建时间
                if (StrUtil.isNotBlank(searchVo.getStartDate()) && StrUtil.isNotBlank(searchVo.getEndDate())) {
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }
                // 模糊搜素
                if (StrUtil.isNotBlank(city.getCityCode())) {
                    list.add(cb.like(cityCode, '%' + city.getCityCode() + '%'));
                }

                // 模糊搜素
                if (StrUtil.isNotBlank(city.getCityName())) {
                    list.add(cb.like(cityName, '%' + city.getCityName() + '%'));
                }

                // 模糊搜素
                if (StrUtil.isNotBlank(city.getProvinceCode())) {
                    list.add(cb.like(provinceCode, '%' + city.getProvinceCode() + '%'));
                }
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }

    @Override
    public CityVo getCityVoLevel(String cityCode) {
        return cityDao.getCityVoLevel(cityCode);
    }

    @Override
    public List<CityVo> getCityVoLevel(List<String> cityCodes) {
        return cityDao.getCityVoLevel(cityCodes);
    }

    @Override
    public HashMap<String, CityVo> getCityVoLevelMap(List<String> cityCodes) {
        List<CityVo> cityVos = cityDao.getCityVoLevel(cityCodes);
        HashMap<String, CityVo> cityMap = new HashMap<>();
        for (CityVo cityVo : cityVos) {
            cityMap.put(cityVo.getCityCode(), cityVo);
        }
        return cityMap;
    }

    @Override
    public HashMap<String, CityVo> getCityVoLevelMap(Set<String> cityCodes) {
        List<String> citys = new ArrayList<>();
        for(String city:cityCodes){
            citys.add(city);
        }
        return getCityVoLevelMap(citys);
    }


}